package com.maimao.goods.service;

import com.maimao.core.exception.MaiMaoDaoException;
import com.maimao.goods.mapper.SpecGroupMapper;
import com.maimao.model.goods.SpecGroup;
import com.maimao.model.goods.input.SpecOrderInput;
import com.maimao.model.goods.vo.SpecGroupEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/12/11 8:18 下午
 */
@Service
public class SpecGroupService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    public List<SpecGroup> selectByCateId(Integer cid3) {

        return specGroupMapper.selectByCateId(cid3);
    }

    /**
     * 修改规格组排序
     * @param input 规格组id
     */
    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void updateOrder(SpecOrderInput input) {
        SpecGroup target = specGroupMapper.selectById(input.getId());
        SpecGroup updateData=null;
        if(input.getUp()){
            //上移
            SpecGroup topOrder = specGroupMapper.selectTopOrder(input.getCid3(), target.getOrder());
            if (topOrder==null){
                return;
            }
            updateData=topOrder;
        }else {
            //下移
            SpecGroup nextOrder = specGroupMapper.selectNextOrder(input.getCid3(), target.getOrder());
            if (nextOrder==null){
                return;
            }
            updateData=nextOrder;
        }
        specGroupMapper.updateGroupOrder(input.getId(),updateData.getOrder());
        specGroupMapper.updateGroupOrder(updateData.getId(),target.getOrder());

    }

    /**
     * 修改规格组名
     */
    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void updateGroupName(SpecGroup specGroup) {
        specGroupMapper.updateGroupName(specGroup.getId(),specGroup.getName());
    }

    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void addGroup(SpecGroup specGroup) {
        specGroupMapper.insert(specGroup);
    }

    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void updateGroup(SpecGroup specGroup) {
        specGroupMapper.updateById(specGroup);
    }

    /**
     * 根据三级分类id查询规格组+组下规格详细信息
     */
    public List<SpecGroupEx>specGroupExList(Integer categoryId){
       return specGroupMapper.selectExByCateId(categoryId);
    }


}
